import { $C, $rp } from '@/utils/constants';

const noteP = $C.notifications.position;

export default (note) => {
  const errs = ['评论提醒参数错误'];
  if (note.authorInfo.err) errs.push(note.authorInfo.msg);
  if (note.spaceInfo.err) errs.push(note.spaceInfo.msg);
  if (note.groupInfo.err) errs.push(note.groupInfo.msg);
  if (note.commentInfo.err) errs.push(note.commentInfo.msg);
  if (note.noteInfo.err) errs.push(note.noteInfo.msg);

  if (errs.length > 1) return { id: note.id, err: true, msg: errs.join(', ') };

  let [desc, rootPath, notePath] = [];
  const content = note.commentInfo.content;
  const parentContent = note.commentInfo.parentContent;
  const spacePath = `/${$rp.SPACES}/${note.spaceInfo.id}`;
  // eslint-disable-next-line max-len
  // const docPath = Base64.encodeURI(`${note.noteInfo.parentPath || ''}/${note.noteInfo.id}-${note.noteInfo.title}`);

  switch (note.noteInfo.position) {
    case noteP.doc: // 资源下的评论
      desc = '评论了我的资源';
      rootPath = `${spacePath}/resources-docs/${note.noteInfo.id}`;
      break;
    case noteP.issue: // 问题下的评论
      desc = '评论了我的问题';
      rootPath = `${spacePath}/${$rp.SPACE_ISSUES}/${note.noteInfo.id}`;
      break;
    case noteP.ctrb: // 贡献下的评论
      desc = '评论了我的贡献';
      rootPath = `${spacePath}/${$rp.SPACE_CONTRIBUTIONS}/${note.noteInfo.id}`;
      break;
    case noteP.groupAnnc: // 群组公告下的评论
      desc = '回复了公告';
      rootPath = `/groups/${note.groupInfo.id}/announcements/${note.noteInfo.id}`;
      break;
    default: errs.push('错误的消息位置position');
  }

  // 楼层参数
  if (!note.commentInfo.parentId) { // 当不是楼中楼时
    const mainFloorPage = parseInt(note.commentInfo.floor / 10, 10) + 1; // 主楼在第几页
    notePath = `${rootPath}?page=${mainFloorPage}&comment_id=${note.commentInfo.id}`;
  } else {
    const mainFloorPage = parseInt(note.commentInfo.parentFloor / 10, 10) + 1; // 主楼在第几页
    const subFloorPage = parseInt(note.commentInfo.floor / 10, 10) + 1; // 楼中楼在第几页
    desc = '回复了你的评论';
    notePath = `${rootPath}?page=${mainFloorPage}&comment_id=${note.commentInfo.parentId}&sub_page=${subFloorPage}&sub_comment_id=${note.commentInfo.id}`;
  }

  return {
    type: note.type,
    author: note.authorInfo,
    desc,
    content,
    parentContent,
    root: { title: note.noteInfo.title, path: rootPath },
    notePath, // 跳转到具体位置的路由

    id: note.id,
    read: note.read,
    created_at: note.created_at,
    updated_at: note.updated_at,
  };
};
